﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Threading.Tasks;
using System.Web.UI.WebControls;

namespace GPS.Controllers
{
    public class GPS_LopHocController : FDBController
    {

        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        //
        // GET: /GPS_LopHoc/
        public ActionResult IndexClass(ViewModelDKH_LOP SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<DKH_LOP>(o => (SearchModel.pKH_LOP_MA == null || o.KH_LOP_MA.StartsWith(SearchModel.pKH_LOP_MA))
                                                             && (SearchModel.pKH_LOP_DVGTVT_MA == null || o.KH_LOP_DVGTVT_MA == SearchModel.pKH_LOP_DVGTVT_MA)
                                                             && (SearchModel.pKH_LOP_HANGDT_MA == null || o.KH_LOP_HANGDT_MA == SearchModel.pKH_LOP_HANGDT_MA)
                                                             && (SearchModel.pKH_LOP_KH_MA == null || o.KH_LOP_KH_MA.StartsWith(SearchModel.pKH_LOP_KH_MA))
                                                             && o.KH_LOP_ACTIVE == "1"
                                                             );

            var model = new List<DKH_LOP>();
            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<DKH_LOP>(currentEntity));
            }

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            ViewBag.TotalRow = entities.Count;

            return View(SearchModel);
        }

        #region Create
        public ActionResult CreateClass()
        {
            initialCategoryCreateAction();

            return View();
        }

        [HttpPost]
        public ActionResult CreateClass(CrudModelDKH_LOP model)
        {
            if (ModelState.IsValid)
            {

                DKH_LOP _model = new DKH_LOP();
                GPS.COMMON.Helpers.CopyObject<DKH_LOP>(model, ref _model, new List<string> { "KH_LOP_ACTIVE" });
                _model.KH_LOP_ACTIVE = (model.KH_LOP_ACTIVE == false ? "0" : "1");
                _model.ID = EFSequence.GetNextValue("SEQ_DKH_LOP_ID");
                if (!string.IsNullOrWhiteSpace(model.KH_LOP_DVGTVT_MA))
                {
                    _model.KH_LOP_DVGTVT_MA = model.KH_LOP_DVGTVT_MA.Split(':')[0];
                }

                _model.KH_LOP_NGUOINHAP = User.Identity.Name;
                _model.KH_LOP_NGAYNHAP = DateTime.Now;

                _db.Insert<DKH_LOP>(_model);

                return RedirectToAction("IndexClass");
            }

            return View(model);
        }

        public ActionResult CreateClassAuto()
        {
            initialCategoryCreateAction();

            return View();
        }

        [HttpPost]
        public ActionResult CreateClassAuto(CrudModelDKH_LOP model)
        {
            //thực hiện tạo lớp ở đây
            var sumHocviens = _db.GetTable<DHV>().Where(o => o.HV_KH_MA == model.KH_LOP_KH_MA && o.HV_DVGTVT_MA == model.KH_LOP_DVGTVT_MA)
                     .GroupBy(g => g.HV_HANGDT_MA)
                     .Select(r => new
                     {
                         HV_HANGDT_MA = r.Key,
                         XEPLICH_NUMBER_HOCVIEN = r.Count()
                     }).ToDictionary(o => o.HV_HANGDT_MA);// hạng làm key


            if (sumHocviens != null && sumHocviens.Count > 0)
            {
                Dictionary<string, int> GroupByHocVien = new Dictionary<string, int>();

                for (int i = 0; i < sumHocviens.Keys.Count; i++)
                {
                    GroupByHocVien.Add(sumHocviens.Keys.ToList()[i], sumHocviens[sumHocviens.Keys.ToList()[i]].XEPLICH_NUMBER_HOCVIEN);
                }
                model.GroupByHocVien = GroupByHocVien;
            }

            //Thuật toán xếp lớp:
            //Get học vien theo khóa học + hạng 

            if (model.GroupByHocVien != null && model.GroupByHocVien.Count > 0)
            {
                foreach (KeyValuePair<string, int> entry in model.GroupByHocVien)
                {
                    var _MAX_HOCVIEN = 30;
                    var _MIN_HOCVIEN = 1;
                    var _KH_MA = model.KH_LOP_KH_MA;
                    var _HANGDT_MA = entry.Key;
                    var _COUNT = entry.Value;
                    List<DHV> _lstDHV = _db.Filter<DHV>(o => o.HV_KH_MA == _KH_MA && o.HV_HANGDT_MA == _HANGDT_MA && o.HV_DVGTVT_MA == model.KH_LOP_DVGTVT_MA && (o.HV_KH_LOP_MA == "CXL" || o.HV_KH_LOP_MA == null));

                    //chia lớp theo từng hạng:
                    List<List<DHV>> _lstDHV_LOP = COMMON.Helpers.splitList<DHV>(_lstDHV, _MAX_HOCVIEN);

                    //Thực hiện sinh mã lớp và insert vào DB:
                    //1. Insert vào bảng lớp DKH_LOP
                    for (int i = 0; i < _lstDHV_LOP.Count; i++)
                    {
                        //create lớp
                        var _DKH = _db.GetOne<DKH>(o => o.KH_MA == _KH_MA);
                        DKH_LOP _DKH_LOP = new DKH_LOP();
                        _DKH_LOP.ID = EFSequence.GetNextValue("SEQ_DKH_LOP_ID");

                        _DKH_LOP.KH_LOP_MA = this.CreateDKH_LOP_MA(_KH_MA);
                        _DKH_LOP.KH_LOP_DVGTVT_MA = _DKH.KH_DVGTVT_MA;
                        _DKH_LOP.KH_LOP_KH_MA = _KH_MA;
                        _DKH_LOP.KH_LOP_TEN = _HANGDT_MA + "K" + (i + 1).ToString();
                        _DKH_LOP.KH_LOP_HANGDT_MA = _HANGDT_MA;
                        var _SHANGDT = _db.GetOne<SHANGDT>(o => o.HANGDT_MA == _HANGDT_MA);
                        _DKH_LOP.KH_LOP_HGPLX_MA = _SHANGDT.HANGDT_HGPLX_MA;
                        _DKH_LOP.KH_LOP_NGAYBD = _DKH.KH_NGAYBD;
                        _DKH_LOP.KH_LOP_NGAYKT = _DKH.KH_NGAYKT;
                        _DKH_LOP.KH_LOP_TONGHV = _lstDHV_LOP[i].Count;
                        _DKH_LOP.KH_LOP_ACTIVE = "1";
                        _DKH_LOP.KH_LOP_NGUOINHAP = User.Identity.Name;
                        _DKH_LOP.KH_LOP_NGAYNHAP = DateTime.Now;

                        _db.Insert<DKH_LOP>(_DKH_LOP);

                        //thêm vào để hiển thị ra list
                        if (model.SearchResults == null)
                            model.SearchResults = new List<DKH_LOP>();
                        model.SearchResults.Add(_DKH_LOP);


                        //2. Insert vào bảng DKH_LOP_HV
                        var _lstDHV_HANGDT = _lstDHV_LOP[i];
                        for (int j = 0; j < _lstDHV_HANGDT.Count; j++)
                        {
                            DKH_LOP_HV _DKH_LOP_HV = new DKH_LOP_HV();
                            _DKH_LOP_HV.ID = EFSequence.GetNextValue("SEQ_DKH_LOP_HV_ID");
                            _DKH_LOP_HV.KH_LOP_HV_KH_LOP_MA = _DKH_LOP.KH_LOP_MA;
                            _DKH_LOP_HV.KH_LOP_HV_DVGTVT_MA = _DKH_LOP.KH_LOP_DVGTVT_MA;
                            _DKH_LOP_HV.KH_LOP_HV_KH_MA = _KH_MA;
                            _DKH_LOP_HV.KH_LOP_HV_HV_MA = _lstDHV_HANGDT[j].HV_MA;
                            _DKH_LOP_HV.KH_LOP_HV_NGUOINHAP = User.Identity.Name;
                            _DKH_LOP_HV.KH_LOP_HV_NGAYNHAP = DateTime.Now;

                            _db.Insert<DKH_LOP_HV>(_DKH_LOP_HV);

                            var id1 = _lstDHV_HANGDT[j].HV_MA;
                            var id2 = _DKH_LOP.KH_LOP_DVGTVT_MA;
                            var _dhvDXL = _db.GetOne<DHV>(o => o.HV_MA == id1 && o.HV_DVGTVT_MA == id2);
                            _dhvDXL.HV_KH_LOP_MA = _DKH_LOP.KH_LOP_MA;
                            _db.Update<DHV>(_dhvDXL);
                        }



                        //Update trạng thái DA_XEP_LOP =1 vào bảng DHV
                    }


                }
            }


            return RedirectToAction("IndexClass");
        }




        //Tạo mã lớp học:
        public string CreateDKH_LOP_MA(string KH_MA)
        {
            var maxDKH_LOP_MA = _db.Filter<DKH_LOP>(o => o.KH_LOP_KH_MA == KH_MA).OrderByDescending(o => o.KH_LOP_MA).FirstOrDefault();
            string strDKH_LOP_MA = KH_MA + "001";

            if (maxDKH_LOP_MA != null)
            {
                var curDKH_LOP_MA = int.Parse(maxDKH_LOP_MA.KH_LOP_MA.Substring(KH_MA.Length, 3)) + 1;
                strDKH_LOP_MA = KH_MA + (curDKH_LOP_MA < 10 ? "00" + curDKH_LOP_MA.ToString() : (curDKH_LOP_MA < 100 ? "0" + curDKH_LOP_MA.ToString() : curDKH_LOP_MA.ToString()));
            }

            return strDKH_LOP_MA;
        }

        #endregion

        #region Edit
        public ActionResult EditClass(decimal id)
        {

            DKH_LOP model = _db.GetOne<DKH_LOP>(o => o.ID == id);

            CrudModelDKH_LOP _model = new CrudModelDKH_LOP();
            GPS.COMMON.Helpers.CopyObject<CrudModelDKH_LOP>(model, ref _model, new List<string> { "KH_LOP_ACTIVE" });
            _model.KH_LOP_ACTIVE = (model.KH_LOP_ACTIVE == "0" ? false : true);
            var dvgtvt = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == model.KH_LOP_DVGTVT_MA);
            _model.KH_LOP_DVGTVT_MA = dvgtvt.DVGTVT_MA + ":" + dvgtvt.DVGTVT_TEN;

            initialCategoryEditAction(_model);
            return View(_model);

        }

        [HttpPost]
        public ActionResult EditClass(CrudModelDKH_LOP model)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<DKH_LOP>(o => o.ID == model.ID);
                GPS.COMMON.Helpers.CopyObject<DKH_LOP>(model, ref _model, new List<string> { "KH_LOP_ACTIVE", "KH_LOP_NGUOINHAP", "KH_LOP_NGAYNHAP" });
                _model.KH_LOP_ACTIVE = model.KH_LOP_ACTIVE == false ? "0" : "1";

                if (!string.IsNullOrWhiteSpace(model.KH_LOP_DVGTVT_MA))
                {
                    _model.KH_LOP_DVGTVT_MA = model.KH_LOP_DVGTVT_MA.Split(':')[0];
                }

                _model.KH_LOP_NGUOISUA = User.Identity.Name;
                _model.KH_LOP_NGAYSUA = DateTime.Now;

                _db.Update<DKH_LOP>(_model);
                return RedirectToAction("IndexClass");
            }

            return View(model);
        }


        #endregion

        #region "Ajax"
        [HttpPost]
        public ActionResult getKH_MAs(string ma_DVGT)
        {
            string _ma_DVGT = ma_DVGT.Split(':')[0];
            var districts = _db.Filter<DKH>(d => d.KH_DVGTVT_MA == _ma_DVGT).Select(a => "<option value='" + a.KH_MA + "'>" + a.KH_MA + "</option>");

            return Content(string.Join("", districts));
        }

        [HttpPost]
        public JsonResult getStatistic(string p_DVGTVT_MA, string p_KH_MA)
        {
            //Thông kê thông tin học viên và hạng
            var sumHocviens = _db.GetTable<DHV>().Where(o => o.HV_KH_MA == p_KH_MA && o.HV_DVGTVT_MA == p_DVGTVT_MA && (o.HV_KH_LOP_MA == null || o.HV_KH_LOP_MA == "CXL"))
                     .GroupBy(g => g.HV_HANGDT_MA)
                     .Select(r => new
                     {
                         HV_HANGDT_MA = r.Key,
                         XEPLICH_NUMBER_HOCVIEN = r.Count()
                     }).ToDictionary(o => o.HV_HANGDT_MA);// hạng làm key

            Dictionary<string, string> GroupByHocVien = null;
            if (sumHocviens != null && sumHocviens.Count > 0)
            {
                GroupByHocVien = new Dictionary<string, string>();

                for (int i = 0; i < sumHocviens.Keys.Count; i++)
                {
                    GroupByHocVien.Add(sumHocviens.Keys.ToList()[i], sumHocviens[sumHocviens.Keys.ToList()[i]].XEPLICH_NUMBER_HOCVIEN.ToString());
                }

            }

            //model.KH_LOP_KH_MA = id1;
            //model.SearchResults = _db.Filter<DKH_LOP>(o => o.KH_LOP_KH_MA == id1);

            return Json(GroupByHocVien);
        }

        [HttpPost]
        public ActionResult getKH_LOP_MA(string p_DVGTVT_MA, string p_KH_LOP_KH_MA)
        {
            var maxDKH_LOP_MA = _db.Filter<DKH_LOP>(o => o.KH_LOP_DVGTVT_MA == p_DVGTVT_MA && o.KH_LOP_KH_MA == p_KH_LOP_KH_MA).OrderByDescending(o => o.KH_LOP_MA).FirstOrDefault();
            string strDKH_LOP_MA = p_KH_LOP_KH_MA + "001";

            if (maxDKH_LOP_MA != null)
            {
                var curDKH_LOP_MA = int.Parse(maxDKH_LOP_MA.KH_LOP_MA.Substring(p_KH_LOP_KH_MA.Length, 3)) + 1;
                strDKH_LOP_MA = p_KH_LOP_KH_MA + (curDKH_LOP_MA < 10 ? "00" + curDKH_LOP_MA.ToString() : (curDKH_LOP_MA < 100 ? "0" + curDKH_LOP_MA.ToString() : curDKH_LOP_MA.ToString()));
            }

            return Content(string.Join("", strDKH_LOP_MA));
        }

        [HttpPost]
        public ActionResult getDGV_HANGHT(string p_HANGHT_MA, string p_DVGTVT_MA)
        {
            string str = "";
            if (!string.IsNullOrWhiteSpace(p_DVGTVT_MA))
                str = p_DVGTVT_MA.Split(':')[0];
            var _RANK = _db.GetOne<SHANGDT>(o => o.HANGDT_MA == p_HANGHT_MA).HANGDT_RANK;
            var DGVs = _db.Filter<DGV>(o => o.GV_HANGDT_RANK > _RANK && o.GV_DVGTVT_MA == str);
            var ss = DGVs.Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HODEM + " " + a.GV_TEN + "</option>");

            return Content(string.Join("", ss));
        }


        public JsonResult AutoCompleteDVGTVT_MA(string term)
        {
            var result = _db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && (o.DVGTVT_MA.Contains(term) || o.DVGTVT_TEN.ToUpper().Contains(term.ToUpper())));
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult getHocVien(string keysearch, string txtKH_LOP_MA, string txtKH_LOP_KH_MA, string txtKH_LOP_DVGTVT_MA, string txtKH_LOP_HANGDT_MA)
        {
            var _keysearh = keysearch.ToUpper();
            var john = _db.Filter<DHV>(o => (o.HV_KH_MA == txtKH_LOP_KH_MA && o.HV_DVGTVT_MA == txtKH_LOP_DVGTVT_MA && o.HV_HANGDT_MA == txtKH_LOP_HANGDT_MA && o.HV_KH_LOP_MA == "CXL")
                                            && (o.HV_HODEM.ToUpper().Contains(_keysearh) || o.HV_TEN.ToUpper().Contains(_keysearh))).Take(15).ToList();

            return Json(new { hocviens = john }, JsonRequestBehavior.AllowGet);

        }

        #endregion


        #region Delete

        public ActionResult DeleteClass(decimal id)
        {


            var _model = _db.GetOne<DKH_LOP>(o => o.ID == id);

            _model.KH_LOP_ACTIVE = "0";
            _db.Update<DKH_LOP>(_model);

            return RedirectToAction("IndexClass");
        }

        #endregion

        #region LopHoc-Student



        public ActionResult IndexStudent(ViewModelDKH_LOP_HV SearchModel)
        {
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");


            ViewBag.pKH_LOP_HV_KH_LOP_MA = SearchModel.pKH_LOP_HV_KH_LOP_MA;

            //var entities = _db.Filter<DKH_LOP_HV>(o => (SearchModel.pKH_LOP_HV_KH_MA == null || o.KH_LOP_HV_KH_MA == SearchModel.pKH_LOP_HV_KH_MA)
            //                                                 && (SearchModel.pKH_LOP_HV_DVGTVT_MA == null || o.KH_LOP_HV_DVGTVT_MA == SearchModel.pKH_LOP_HV_DVGTVT_MA)
            //                                                 && (SearchModel.pKH_LOP_HV_KH_LOP_MA == null || o.KH_LOP_HV_KH_LOP_MA == SearchModel.pKH_LOP_HV_KH_LOP_MA)
            //                                                 );
            var entities = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GetdataHocVienInLop(SearchModel.pKH_LOP_HV_DVGTVT_MA
                                                                                               , SearchModel.pKH_LOP_HV_KH_MA
                                                                                               , SearchModel.pKH_LOP_HV_KH_LOP_MA
                                                                                              );
            //var model = new List<DKH_LOP_HV>();
            //foreach (var currentEntity in entities)
            //{
            //    model.Add(Mapper.Map<DKH_LOP_HV>(currentEntity));
            //}


            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = entities.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            if (entities != null)
                ViewBag.TotalRow = entities.Count;

            return View(SearchModel);

        }

        public ActionResult CreateStudent(string id1, string id2, string id3, int? currentPage)
        {
            try
            {
                string _KH_LOP_HV_KH_LOP_MA = id1;
                string _KH_LOP_HV_DVGTVT_MA = id2;
                string _KH_LOP_HV_KH_MA = id3;


                DKH_LOP model = _db.GetOne<DKH_LOP>(o => o.KH_LOP_MA == id1 && o.KH_LOP_DVGTVT_MA == id2 && o.KH_LOP_KH_MA == id3);// await _db.GetDbSet<DKH_LOP>().FindAsync(id1, id2, id3); //_db.GetOne<DKH_LOP>(o => o.KH_LOP_MA == id1 && o.KH_LOP_DVGTVT_MA == id2 && o.KH_LOP_KH_MA == id3);

                if (model == null)
                {
                    return HttpNotFound();
                }
                CrudModelDKH_LOP _model = new CrudModelDKH_LOP();
                GPS.COMMON.Helpers.CopyObject<CrudModelDKH_LOP>(model, ref _model, new List<string> { "SearchResults", "Search_HV_Results", "GroupByHocVien", "KH_LOP_ACTIVE" });

                var pageIndex = currentPage ?? 1;
                _model.Search_HV_Results = _db.Filter<DHV>(o => (o.HV_KH_LOP_MA == "CXL" || o.HV_KH_LOP_MA == null) && o.HV_HANGDT_MA == model.KH_LOP_HANGDT_MA && o.HV_DVGTVT_MA == _KH_LOP_HV_DVGTVT_MA && o.HV_KH_MA == model.KH_LOP_KH_MA).ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

                return PartialView("_AddStudentClass", _model);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }


       
        public ActionResult List(string id1, string id2, string id3, int? page)
        {
            CrudModelDKH_LOP _model = new CrudModelDKH_LOP();
            DKH_LOP model = _db.GetOne<DKH_LOP>(o => o.KH_LOP_MA == id1 && o.KH_LOP_DVGTVT_MA == id2 && o.KH_LOP_KH_MA == id3);

            int pageSize = 10;
            int pageNumber = (page ?? 1);
            _model.Search_HV_Results = _db.Filter<DHV>(o => o.HV_KH_LOP_MA == "CXL" && o.HV_HANGDT_MA == model.KH_LOP_HANGDT_MA).ToPagedList(pageNumber, pageSize);
            return PartialView("_AddStudentClass", _model);
        }

        [HttpPost]
        public ActionResult CreateStudent(CrudModelDKH_LOP model, FormCollection _form)
        {

            int _count = 0;
            foreach (var _key in _form.AllKeys)
            {
                List<string> lstMA = null;
                if (_key.StartsWith("chk_"))
                {
                    lstMA = _form[_key].Split('#').ToList();
                    DKH_LOP_HV _itemInsert = new DKH_LOP_HV();
                    _itemInsert.ID = EFSequence.GetNextValue("SEQ_DKH_LOP_HV_ID");
                    _itemInsert.KH_LOP_HV_KH_LOP_MA = _form["KH_LOP_MA"].ToString(); //lstMA[0];
                    _itemInsert.KH_LOP_HV_DVGTVT_MA = lstMA[1];
                    _itemInsert.KH_LOP_HV_KH_MA = lstMA[2];
                    _itemInsert.KH_LOP_HV_HV_MA = lstMA[3];
                    _itemInsert.KH_LOP_HV_NGAYNHAP = DateTime.Now;
                    _itemInsert.KH_LOP_HV_NGUOINHAP = User.Identity.Name;

                    //insert vao bảng DKH_LOP_HV
                    _db.Insert<DKH_LOP_HV>(_itemInsert);
                    //update trạng thái bảng DHV
                    DHV _dhv = _db.GetOne<DHV>(o => o.HV_MA == _itemInsert.KH_LOP_HV_HV_MA && o.HV_DVGTVT_MA == _itemInsert.KH_LOP_HV_DVGTVT_MA);
                    _dhv.HV_KH_LOP_MA = _itemInsert.KH_LOP_HV_KH_LOP_MA;
                    _db.Update<DHV>(_dhv);

                    _count++;
                }

            }

            TempData["ThongBao"] = string.Format("Đã thêm [{0}] học viên hạng đào tạo [{1}] vào lớp [{2}] thành công!", _count, model.KH_LOP_HANGDT_MA, model.KH_LOP_MA);

            string url = Url.Action("IndexStudent", "GPS_LopHoc", new { pKH_LOP_HV_KH_LOP_MA = model.KH_LOP_MA, pKH_LOP_HV_DVGTVT_MA = model.KH_LOP_DVGTVT_MA, pKH_LOP_HV_KH_MA = model.KH_LOP_KH_MA, pKH_LOP_HANGDT_MA = model.KH_LOP_HANGDT_MA });

            return Json(new { success = true, url = url });

            //}
            //else
            //{
            //   return PartialView("_AddStudentClass", model);
            //}

        }

        public ActionResult DeleteStudent(string id1, string id2, string id3, string id4)
        {
            var KH_LOP_HV_KH_LOP_MA = id1;
            var KH_LOP_HV_DVGTVT_MA = id2;
            var KH_LOP_HV_KH_MA = id3;
            var KH_LOP_HV_HV_MA = id4;
            //delete bang DKH_LOP_HV
            _db.Delete<DKH_LOP_HV>(o => o.KH_LOP_HV_KH_LOP_MA == id1 && o.KH_LOP_HV_DVGTVT_MA == id2 && o.KH_LOP_HV_KH_MA == id3 && o.KH_LOP_HV_HV_MA == id4);
            //update bang DHV :
            //update trạng thái bảng DHV
            DHV _dhv = _db.GetOne<DHV>(o => o.HV_MA == id4 && o.HV_DVGTVT_MA == id2);
            _dhv.HV_KH_LOP_MA = "CXL";
            _db.Update<DHV>(_dhv);

            //get hangdt:
            var _HANGHT = _db.GetOne<DKH_LOP>(o => o.KH_LOP_MA == id1 && o.KH_LOP_DVGTVT_MA == id2 && o.KH_LOP_KH_MA == id3).KH_LOP_HANGDT_MA;
            TempData["ThongBao"] = string.Format("Đã xóa [{0}] học viên hạng đào tạo [{1}] khỏi lớp [{2}] thành công!", 1, _HANGHT, KH_LOP_HV_KH_LOP_MA);
            return RedirectToAction("IndexStudent", new { pKH_LOP_HV_KH_LOP_MA = id1, pKH_LOP_HV_DVGTVT_MA = id2, pKH_LOP_HV_KH_MA = id3, pKH_LOP_HANGDT_MA = _HANGHT });
        }


        public ActionResult EditStudent(string id1, string id2, string id3, string id4, string id5)
        {
            string _KH_LOP_HV_KH_LOP_MA = id1;
            string _KH_LOP_HV_DVGTVT_MA = id2;
            string _KH_LOP_HV_KH_MA = id3;
            string _KH_LOP_HV_HV_MA = id4;
            ViewBag.HANGDT_MA = id5;

            ViewBag.DKH_LOP_MAs = new SelectList(_db.Filter<DKH_LOP>(o => o.KH_LOP_MA != _KH_LOP_HV_KH_LOP_MA && o.KH_LOP_HANGDT_MA == id5), "KH_LOP_MA", "KH_LOP_TEN");

            DKH_LOP_HV model = _db.GetOne<DKH_LOP_HV>(o => o.KH_LOP_HV_KH_LOP_MA == id1 && o.KH_LOP_HV_DVGTVT_MA == id2 && o.KH_LOP_HV_KH_MA == id3 && o.KH_LOP_HV_HV_MA == id4);
            if (model == null)
            {

                return HttpNotFound();
            }
            CrudModelDKH_LOP_HV _model = new CrudModelDKH_LOP_HV();
            GPS.COMMON.Helpers.CopyObject<CrudModelDKH_LOP_HV>(model, ref _model);
            _model.KH_LOP_HV_KH_LOP_MA_OLD = _model.KH_LOP_HV_KH_LOP_MA;

            return PartialView("_ChangeStudentClass", _model);

        }

        [HttpPost]
        public ActionResult EditStudent(CrudModelDKH_LOP_HV model, FormCollection form)
        {
            if (ModelState.IsValid)
            {

                var _modelDelete = _db.GetOne<DKH_LOP_HV>(o => o.KH_LOP_HV_HV_MA == model.KH_LOP_HV_HV_MA && o.KH_LOP_HV_KH_LOP_MA == model.KH_LOP_HV_KH_LOP_MA_OLD && o.KH_LOP_HV_KH_MA == model.KH_LOP_HV_KH_MA && o.KH_LOP_HV_DVGTVT_MA == model.KH_LOP_HV_DVGTVT_MA);

                GPS.COMMON.Helpers.CopyObject<CrudModelDKH_LOP_HV>(_modelDelete, ref model, new List<string> { "KH_LOP_HV_KH_LOP_MA" });
                //remove
                _db.Delete<DKH_LOP_HV>(o => o.KH_LOP_HV_HV_MA == _modelDelete.KH_LOP_HV_HV_MA && o.KH_LOP_HV_KH_LOP_MA == _modelDelete.KH_LOP_HV_KH_LOP_MA && o.KH_LOP_HV_KH_MA == _modelDelete.KH_LOP_HV_KH_MA && o.KH_LOP_HV_DVGTVT_MA == _modelDelete.KH_LOP_HV_DVGTVT_MA);

                //insert
                var _modelInsert = new DKH_LOP_HV();

                GPS.COMMON.Helpers.CopyObject<DKH_LOP_HV>(model, ref _modelInsert);

                _db.Insert<DKH_LOP_HV>(_modelInsert);

                TempData["ThongBao"] = string.Format("Đã chuyển học viên [{0}] từ lớp [{1}] sang lớp [{2}] thành công!", _modelInsert.KH_LOP_HV_HV_MA, model.KH_LOP_HV_KH_LOP_MA_OLD, model.KH_LOP_HV_KH_LOP_MA);
                ViewBag.DKH_LOP_MAs = new SelectList(_db.Filter<DKH_LOP>(o => o.KH_LOP_MA != model.KH_LOP_HV_KH_LOP_MA_OLD), "KH_LOP_MA", "KH_LOP_TEN");

                //   return RedirectToAction("IndexStudent", new { pKH_LOP_HV_KH_LOP_MA = model.KH_LOP_HV_KH_LOP_MA_OLD, pKH_LOP_HV_DVGTVT_MA = model.KH_LOP_HV_DVGTVT_MA, pKH_LOP_HV_KH_MA = model.KH_LOP_HV_KH_MA });
                string url = Url.Action("IndexStudent", "GPS_LopHoc", new { pKH_LOP_HV_KH_LOP_MA = model.KH_LOP_HV_KH_LOP_MA_OLD, pKH_LOP_HV_DVGTVT_MA = model.KH_LOP_HV_DVGTVT_MA, pKH_LOP_HV_KH_MA = model.KH_LOP_HV_KH_MA });

                return Json(new { success = true, url = url });

            }
            else
            {
                var id5 = form["HANGDT_MA"];
                ViewBag.HANGDT_MA = id5;
                ViewBag.DKH_LOP_MAs = new SelectList(_db.Filter<DKH_LOP>(o => o.KH_LOP_MA != model.KH_LOP_HV_KH_LOP_MA_OLD && o.KH_LOP_HANGDT_MA == id5), "KH_LOP_MA", "KH_LOP_TEN");
                return PartialView("_ChangeStudentClass", model);
            }

        }


        #endregion

        #region Function
        public void initialCategorySearchAction(ViewModelDKH_LOP viewModel)
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");

        }

        public void initialCategoryCreateAction()
        {
            // ApplicationUser curUser = getCurrentUser();
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");
            ViewBag.HANGGPLX_MAs = new SelectList(_db.GetAll<SHANGGPLX>(), "HGPLX_MA", "HGPLX_TEN");
            ViewBag.DKH_MAs = new SelectList(_db.GetAll<DKH>(), "KH_MA", "KH_TEN");

            ViewBag.HV_NOICAPCMTs = new SelectList(_db.Filter<SDONVIHC>(o => o.DVHC_MA == "40"), "DVHC_MA", "DVHC_TENDAYDU");
            ViewBag.HV_QUOCTICH_MAs = new SelectList(_db.GetAll<SQUOCTICH>(), "QTICH_MA", "QTICH_TENEN");

            List<DGV> _lstDGV = _db.GetAll<DGV>();
            List<ModelDGV> _lstMODELDGV = new List<ModelDGV>();

            foreach (var item in _lstDGV)
            {
                ModelDGV _m = new ModelDGV();
                GPS.COMMON.Helpers.CopyObject<ModelDGV>(item, ref _m);
                _lstMODELDGV.Add(_m);
            }
            ViewBag.DGV_MAs = new SelectList(_lstMODELDGV, "GV_MA", "GV_TEN");
            ViewBag.DPT_MAs = new SelectList(_db.GetAll<DPT>(), "PT_BIENSO", "PT_BIENSO");
        }

        public void initialCategoryEditAction(CrudModelDKH_LOP view)
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");
            ViewBag.HANGGPLX_MAs = new SelectList(_db.GetAll<SHANGGPLX>(), "HGPLX_MA", "HGPLX_TEN");
            ViewBag.DKH_MAs = new SelectList(_db.GetAll<DKH>(), "KH_MA", "KH_TEN");

            ViewBag.HV_NOICAPCMTs = new SelectList(_db.Filter<SDONVIHC>(o => o.DVHC_MA == "40"), "DVHC_MA", "DVHC_TENDAYDU");
            ViewBag.HV_QUOCTICH_MAs = new SelectList(_db.GetAll<SQUOCTICH>(), "QTICH_MA", "QTICH_TENEN");

            List<DGV> _lstDGV = _db.GetAll<DGV>();
            List<ModelDGV> _lstMODELDGV = new List<ModelDGV>();

            foreach (var item in _lstDGV)
            {
                ModelDGV _m = new ModelDGV();
                GPS.COMMON.Helpers.CopyObject<ModelDGV>(item, ref _m);
                _lstMODELDGV.Add(_m);
            }
            ViewBag.DGV_MAs = new SelectList(_lstMODELDGV, "GV_MA", "GV_HOTEN");
            ViewBag.DPT_MAs = new SelectList(_db.GetAll<DPT>(), "PT_BIENSO", "PT_BIENSO");
        }
        #endregion
    }

    public class Employee
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}